package com.courier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.courier.entity.Package;
import com.courier.mapper.PackageMapper;
import com.courier.service.PackageService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Override
    public boolean inStorage(Package pkg) {
        pkg.setStatus("已入库");
        pkg.setCreateTime(LocalDateTime.now());
        pkg.setUpdateTime(LocalDateTime.now());
        return save(pkg);
    }
    @Override
    public boolean outStorage(Long id) {
        Package pkg = getById(id);
        if (pkg != null && "已入库".equals(pkg.getStatus())) {
            pkg.setStatus("已出库");
            pkg.setUpdateTime(LocalDateTime.now());
            return updateById(pkg);
        }
        return false;
    }
    @Override
    public boolean signFor(Long id) {
        return signFor(id, null);
    }

    @Override
    public boolean signFor(Long id, String signPerson) {
        Package pkg = getById(id);
        if (pkg != null) {
            pkg.setStatus("已签收");
            pkg.setSignTime(LocalDateTime.now());
            pkg.setSignPerson(signPerson);
            pkg.setUpdateTime(LocalDateTime.now());
            return updateById(pkg);
        }
        return false;
    }

    @Override
    public List<Package> findByStatus(String status) {
        return baseMapper.selectByStatus(status);
    }

    @Override
    public List<Package> findByTrackingNumber(String trackingNumber) {
        return baseMapper.selectByTrackingNumber(trackingNumber);
    }

    @Override
    public List<Package> findPage(int page, int pageSize) {
        QueryWrapper<Package> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        int offset = (page - 1) * pageSize;
        return baseMapper.selectPage(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(offset, pageSize),
                wrapper
        ).getRecords();
    }
}